;(function($){
	$.fn.spa_fwf = function(opts){
		var el = this;
		var opt = {
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//关闭
			el.find("#close-dialog,#modalBtnCancel").unbind("click").click(function(){
				el.modal("hide");
				el.empty();
				param.closeBack();
			});
			
			//输入钱数
    		el.find("#fwfl_help").bind("click",function(){
    			//计算器
				var status = false;
				el.find("#modal-dialog").hide();
				if(!$.isFunction($().wechatAlipayPay)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/openSpa/jquery.plugin.digitalCalculator.js");
		            $.ajaxSetup({ async: true });
				}
				el.find("#mykeyboard").digitalCalculator({
					number:el.find("#pay").val()?el.find("#pay").val():1,//数量
					status:status?status:false,
					title:param.title?param.title:"",
					//初始化数据返回回调
					dataBack:function(number){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
 						var str = number.toString();
						str = str.substr(0,str.length-3)+"%";
 						el.find("#fwfl").val(str);
					},
					returnBack :function(){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
					}
				});
    		});
    		
    		//确认按钮
    		el.find("#modalBtnSave").unbind("click").click(function(){
    			t.wait();
    			var postData={};
    			var fwfl = parseInt(el.find("#fwfl").val().replace("%"))/100
    			postData.ct_brzd = {"no":param.no,"discode":"","fwfl":fwfl};
        		$.ajax({
        			url: $.HSAPI_AJAXURL+"/qms/mobile/procedure2",      
        			type: "post",
        			dataType: "json",
        			data: $.HSAPI_ASKJSONINFO(JSON.stringify(postData),'POSCT_brzdEditSave',"{}"),
        		}).success(function (data) {
        			t.goon();
					if(data.RETURN != -1){
    					param.dataBack();
    					el.modal("hide");
    					el.empty();
    				}else{
    					jf_alert(data.MSG);
    				}
        		}).error(function (data) {
        			t.goon();
        			jf_alert(JSON.stringify(data));
        		});
    		})
    		
    		if(!$.isFunction($().mask)){
				$.ajaxSetup({ async: false, cache: false });
	            $.getScript("assets/js/jquery.maskedinput.js");
	            $.ajaxSetup({ async: true });
			}
    		el.find('#fwfl').mask('99%').val(10);
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="mykb-box" id="mykeyboard"></div>'+
						'<div id="modal-dialog" class="modal-dialog" style="width:600px;">'+
							'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="title-dialog">'+$.i18n.propJsp("服务费")+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body">'+
									'<form id="frm" class="form-horizontal">'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("服务费率:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" id="coupon_type" style="width:360px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" id="fwfl" name="fwfl">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="fwfl_help">'+
															'<span class="ace-icon fa fa-keyboard-o bigger-110"></span>'+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</form>'+
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-right">'+
											'<div class="btn-group">'+
												'<button id="modalBtnSave" type="button" class="btn btn-sm btn-info">'+
													'<i class="ace-icon fa fa-check"></i>'+
													$.i18n.propJsp("确认")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancel" type="button" class="btn btn-sm btn-default">'+
													'<i class="ace-icon fa fa-times"></i>'+
													$.i18n.propJsp("返回")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
		},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
			
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},
	}
	
	function checkTwoPoint(value){  		
	    value=value.toString()+".00";
	    value = value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
	    value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	    value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
	    return value;
	}
	
})(jQuery);